#include <iostream>

using namespace std;

class Visitor;

class Animal {
public:
    virtual void accept(Visitor* v) = 0;
    virtual void speak() = 0;
};

class Dog: public Animal {
public:
    void accept(Visitor* v);

    void speak() {
        cout << "wang wang~" << endl;
    }
};

class Cat: public Animal {
public:
    void accept(Visitor* v);

    void speak() {
        cout << "miao miao ~" << endl;
    }
};

class Visitor {
public:
    virtual void visit(Animal* animal)=0;
};

class SpeakVisitor: public Visitor {
public:
    void visit(Animal* a) {
        a->speak();
    }
};

void Dog::accept(Visitor* v) {
    v->visit(this);
}

void Cat::accept(Visitor* v) {
    v->visit(this);
}


int main(int argc, char** argv) {
    Animal* arr[] = {new Dog(), new Cat()};
    Visitor* v = new SpeakVisitor();
    for (int i=0; i<sizeof(arr)/sizeof(Animal*); i++) {
        v->visit(arr[i]);
    }
}
